import requests

from binance.um_futures import UMFutures
from util.mysql_utils import *
import datetime



#只能获取账户信息的 apikey
key = "lSfS73A8jxifi2btCmE7fPnhrg5Zf333pyGZkuuwHvZ7GrZg11gyE52SrfY0Kmms"
secret = "8CUn0fvnmJ7ufDZ0BuVuMzu2jOkOb9vfQ077p3iQTyL6IL6WquQa3AgQ8BIW1cAS"

um_futures_client = UMFutures(key=key, secret=secret)
def notice_user(mysql_util):
    conn = mysql_util.get_connect()
    cur = conn.cursor()
    cur.execute("SELECT * from himalaya_user where user_status = 1")
    #获取 账户名义价值
    account_name_val = get_account_balance()
    user_rows = cur.fetchall()
    total_amount = 0
    for row in user_rows:
        amount = float(row[2])
        total_amount += amount



    for row in user_rows:
        open_id = row[1]
        amount = float(row[2])
        send_msg(open_id,account_name_val,amount,per_portion_val=(account_name_val/total_amount))



url = 'https://kyb-api.520iy.com/public/sendWxMsg'


# user_amount  用户总份额 per_portion_val 每份价值
def send_msg(openid,account_name_val,user_amount,per_portion_val):
    paras = {}
    paras['openid'] = openid
    msg = {}
    paras['msg'] = msg

    msg['type'] = 'income_msg'
    msg['one'] = '您的 持有份额 :' + str(user_amount) + " 每份净值:" + '{:.4f}'.format(per_portion_val)
    total_val = user_amount * per_portion_val
    msg['two'] = '总价值 usdt:' + '{:.4f}'.format(total_val)
    msg['diff'] = ''
    # 当前日期和时间
    current_datetime = datetime.datetime.now()

    # 格式化输出当前日期和时间
    formatted_date = current_datetime.strftime('%Y-%m-%d %H:%M:%S')

    msg['time'] = formatted_date
    response = requests.post(url, json=paras)

    print(response)

def get_account_balance():
    response = um_futures_client.balance(recvWindow=6000)
    for symol_account in response:
        if symol_account['asset'] == 'USDT':
            #资产余额
            crossWalletBalance = float(symol_account['crossWalletBalance'])
            #未实现盈亏
            crossUnPnl = float(symol_account['crossUnPnl'])
            #账户名义价值
            account_name_val = crossWalletBalance + crossUnPnl
            return account_name_val

    return 0

if __name__ == '__main__':
    mysql_utils = mysql_utils("rm-wz99662u81d4b7d6sso.mysql.rds.aliyuncs.com",
                              3306,
                              "suib_tjb",
                              "suib@2024",
                              "himalaya")


    notice_user(mysql_utils)
